// packageA/member/wechatWithdrawal/wechatWithdrawal.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    member: {},
    withdraw: {},
    orderType: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.orderType) {
      this.setData({
        orderType: options.orderType,
      });
    }
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    let mailLanguage = JSON.parse(wx.getStorageSync("mailLanguage"));
    if (mailLanguage.income) {
      let name_of_withdrawal = mailLanguage.income.name_of_withdrawal;
      this.setData({
        name_of_withdrawal: name_of_withdrawal,
      });
      wx.setNavigationBarTitle({
        title: name_of_withdrawal,
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  async getData() {
    let API = "wechat.merchant-transfer.index";
    if (this.data.orderType == "supplier") {
      API = "plugin.supplier.frontend.supplier-wx-withdraw.index";
    } else if (this.data.orderType == "love") {
      API = "plugin.love.Frontend.Modules.Love.Controllers.wx-transfer.receive";
    }
    let { data } = await app._postNetWork({
      url: app.getNetAddresss(API),
      data: {
        id: this.options.id,
      },
    });
    if (data.result) {
      console.log(data);
      if (data.data.withdraw.transfer_status == 1) {
        wx.showModal({
          showCancel: false,
          title: "",
          content: "这笔打款已确认收款",
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1,
              });
            }
          },
        });
        return;
      }
      this.setData({
        withdraw: data.data.withdraw,
        member: data.data.member,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async submit() {
    let API = "wechat.merchant-transfer.withdraw";
    if (this.data.orderType == "supplier") {
      API = "plugin.supplier.frontend.supplier-wx-withdraw.withdraw";
    } else if (this.data.orderType == "love") {
      API =
        "plugin.love.Frontend.Modules.Love.Controllers.wx-transfer.withdraw";
    }
    let { data } = await app._postNetWork({
      url: app.getNetAddresss(API),
      data: {
        id: this.options.id,
      },
    });
    if (data.result) {
      let resData = data.data;
      if (wx.canIUse("requestMerchantTransfer")) {
        wx.requestMerchantTransfer({
          mchId: resData.mch_id,
          appId: resData.app_id,
          package: resData.package_info,
          success: (res) => {
            // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
            console.log("success:", res);
            // wx.navigateBack({
            //   delta: 1
            // })
          },
          fail: (res) => {
            console.log("fail:", res);
          },
        });
      } else {
        wx.showModal({
          content: "你的微信版本过低，请更新至最新版本。",
          showCancel: false,
        });
      }
    } else {
      app.tips(data.msg);
    }
  },
});
